package com.tpp.base.controller;

import com.tpp.base.service.AdminItemTypeService;
import com.tpp.comm.pojo.TpItemType;
import com.tpp.comm.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Slf4j
@RequestMapping("/s/itemType")
public class AdminItemTypeController {

    @Resource
    private AdminItemTypeService adminItemTypeService;

    @GetMapping("/findItemType")
    public Result findItemType(){
        return Result.success(adminItemTypeService.findItemTypes());
    }

    @PostMapping("/add")
    public Result addItemType(@RequestBody TpItemType tpItemType){
        try {
            adminItemTypeService.addItemType(tpItemType);
            return Result.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.fail("添加失败");
        }
    }

    @PostMapping("/update")
    public Result updateItemType(@RequestBody TpItemType tpItemType){
        try {
            adminItemTypeService.updateItemType(tpItemType);
            return Result.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.fail("修改失败");
        }
    }

    @GetMapping("/delete")
    public Result deleteItemType(Integer id){
        try {
            adminItemTypeService.deleteItemType(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.fail("删除失败");
        }
    }

    @GetMapping("/findById")
    public Result findById(Integer id){
        return Result.success(adminItemTypeService.findById(id));
    }
}
